package android.medicalcloud.com.yiapp.yilian.logic;

import android.medicalcloud.com.yiapp.base.cach.PreferceManager;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.MapBuilder;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.application.YilianAppcation;
import android.medicalcloud.com.yiapp.yilian.configer.enums.HttpConfig;
import android.medicalcloud.com.yiapp.yilian.model.AppVersionModel;
import android.medicalcloud.com.yiapp.yilian.model.BannersModel;
import android.medicalcloud.com.yiapp.yilian.model.BaseModeal;
import android.medicalcloud.com.yiapp.yilian.model.ConfigModel;
import android.medicalcloud.com.yiapp.yilian.model.VersionModel;
import android.medicalcloud.com.yiapp.yilian.utils.android.Common;
import android.medicalcloud.com.yiapp.yilian.utils.android.GsonTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.JsonTool;
import android.medicalcloud.com.yiapp.yilian.utils.net.ConnectTool;
import android.util.Log;

import java.io.File;
import java.io.Serializable;

/**
 * Created by wanggenlin on 17-5-18.
 */
public class CommonController {
    static CommonController instance;

    public static CommonController getInstance() {
        if (instance == null) {
            instance = new CommonController();
        }
        return instance;
    }
    /**
     * login
     */
    public void verify(ViewNetCallBack viewNetCallBack, String mobile,String userName) {
        try {
            ConnectTool.httpRequest(
                    HttpConfig.verify,
                    new MapBuilder<String, Object>()
                            .add("mobile",mobile)
                            .add("userName",userName)
                            .getMap()
                    , viewNetCallBack
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

    /**
     * login
     */
    public void verification(ViewNetCallBack viewNetCallBack, String mobile,String verifyCode) {
        try {
            ConnectTool.httpRequest(
                    HttpConfig.verification,
                    new MapBuilder<String, Object>()
                            .add("mobile",mobile)
                            .add("verifyCode",verifyCode)
                            .getMap()
                    , viewNetCallBack
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

    /**
     * login
     */
    public void config() {
        try {
            ConnectTool.httpRequest(
                    HttpConfig.config,
                    new MapBuilder<String, Object>()
                            .getMap()
                    , new CommonRespone()
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

    private  class CommonRespone implements  ViewNetCallBack{
        @Override
        public void onConnectStart(Object o) {

        }

        @Override
        public void onConnectEnd() {

        }

        @Override
        public void onFail(Exception e, Object o, String reslut) {

        }

        @Override
        public void onData(Serializable result, int flag, boolean fromNet, Object o) {

            if (flag==HttpConfig.config.getType()){
                Logger.e(o.toString()+"       fda");
                PreferceManager.getInsance().saveValueBYkey("config", JsonTool.getString(o.toString(),"redirectUrl"));
            }
        }
    }


    public ConfigModel getConfigModel(){
        String reslut =  PreferceManager.getInsance().getValueBYkey("config");

        Logger.e("reslut-----"+reslut);
        try {
            return GsonTool.jsonToEntity(reslut,ConfigModel.class);
        }catch (Exception e){
            return null;
        }


    }





    /**
     * login
     */
    public void ad(ViewNetCallBack viewNetCallBack) {
        try {
            ConnectTool.httpRequest(
                    HttpConfig.ad,
                    new MapBuilder<String, Object>()
                            .add("type",1)
                            .getMap()
                    , viewNetCallBack
                    , BannersModel.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }




    /**
     * login
     */
    public void upLoadImage(ViewNetCallBack viewNetCallBack,String path) {
        Logger.e("path=  ="+path);
        try {
            ConnectTool.httpRequest(
                    HttpConfig.uploadImage,
                    new MapBuilder<String, Object>()
                            .add("file",new File(path))
                            .add("prefix","avatar")
                            .getMap()
                    , viewNetCallBack
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

    /**
     * login
     */
    public void versionCheck(ViewNetCallBack viewNetCallBack) {
        try {
            ConnectTool.httpRequest(
                    HttpConfig.versionUpdate,
                    new MapBuilder<String, Object>()
                            .add("appId", YilianAppcation.instance.getPackageName())
                            .add("OS", "android")
                            .add("version", Common.getManifestVisionInt(YilianAppcation.instance)+"")
                            .getMap()
                    , viewNetCallBack
                    , VersionModel.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

}
